<template>
  <!-- ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上-->
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <!--border-topbottom指定top和bottom有边框  -->
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <!--border-topbottom指定top和bottom有边框  -->
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="item of hot"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <!-- key代表A,B,C,D..... -->
      <div class="area" v-for="(item,key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div
            class="item border-bottom"
            v-for="innerItem of item"
            :key="innerItem.id"
            @click="handleCityClick(innerItem.name)"
          >{{innerItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
import BScroll from "better-scroll";

export default {
  name: "CityList",
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState(["city"]) //展开运算符
    // ...mapState是指我把vuex里面的数据映射到我这个组件的computed这个计算属性里，
    //把city这公共数据映射到我的city的计算属性里
  },
  methods: {
    handleCityClick(city) {
      this.changeCity(city);
      this.$router.push("/");
    },
    ...mapMutations(["changeCity"])
  },
  mounted() {
    //生命周期函数，在dom挂载完毕执行
    this.scroll = new BScroll(this.$refs.wrapper);
  },
  // vue中自带的侦听器,在这里监听letter的变化
  watch: {
    letter() {
      if (this.letter) {
        //注意是this.letter
        const element = this.$refs[this.letter][0];
        //这时element是个数组,要用下标方式取内部内容
        this.scroll.scrollToElement(element); //这是better-scroll提供的接口
        //让它自动的滚到某个元素上
      }
    }
  }
};
</script>
<style lang='stylus' scoped>
@import '~styles/varibles.styl';

// 设置border-topbottom这个类的before和after尾元素的border color
.border-topbottom {
  &:before {
    border-color: #ccc;
  }

  &:after {
    border-color: #ccc;
  }
}

.border-bottom {
  &:before {
    border-color: #ccc;
  }
}

.list {
  overflow: hidden; // 由于撑大出去了一部分
  position: absolute;
  top: 1.58rem; // 把头部的搜索框给留出来
  left: 0;
  right: 0;
  bottom: 0;

  .title {
    line-height: 0.55rem;
    background: #eee;
    padding-left: 0.25rem;
    color: #666;
  }

  .button-list {
    overflow: hidden;
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;

    .button-wrapper {
      float: left;
      width: 33.33%;

      .button {
        padding: 0.1rem 0;
        text-align: center;
        margin: 0.1rem;
        border: 0.02rem solid #ccc;
        border-radius: 0.06rem;
      }
    }
  }

  .item-list {
    .item {
      line-height: 0.76rem;
      color: #666;
      padding-left: 0.2rem;
    }
  }
}
</style>